package com.itmk.web.goods_recive.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.goods.entity.Goods;
import com.itmk.web.goods.service.GoodsService;
import com.itmk.web.goods_recive.entity.GoodsRecive;
import com.itmk.web.goods_recive.entity.GoodsReciveQuery;
import com.itmk.web.goods_recive.service.GoodsReciveService;
import com.itmk.web.wx_user.entity.WxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/api/goodsrevice")
@RestController
public class GoodsReciveController {
    @Autowired
    private GoodsReciveService goodsReciveService;
    @Autowired
    private GoodsService goodsService;

    //创建
    @PostMapping("/release")
    public ResultVo add(@RequestBody GoodsRecive goodsRecive){
        if(goodsReciveService.save(goodsRecive)){
            return ResultUtils.success("发布成功!");
        }
        return ResultUtils.error("发布失败!");
    }

    //列表
    @GetMapping("/list")
    public ResultVo getList(GoodsRecive parm){
        List<GoodsReciveQuery> list=goodsReciveService.getList(parm.getGoodsId());
        return ResultUtils.success("查询成功",list);
    }

    //通过
    @PostMapping("/audit")
    public ResultVo audit(@RequestBody GoodsRecive goodsRecive){
        if(goodsRecive.getAuditType()==1){
            QueryWrapper<Goods> goodsQuery = new QueryWrapper<>();
            goodsQuery.lambda().eq(Goods::getGoodsId,goodsRecive.getGoodsId());
            Goods g = goodsService.getOne(goodsQuery);
            if(g.getSellStatus().equals("1")){
                return ResultUtils.success("已被认领!");
            }
            UpdateWrapper<Goods> goodsUpdateQuery = new UpdateWrapper<>();
            goodsUpdateQuery.lambda().set(Goods::getSellStatus,"1")
                    .eq(Goods::getGoodsId,goodsRecive.getGoodsId());
            goodsService.update(goodsUpdateQuery);
        }

        UpdateWrapper<GoodsRecive> query = new UpdateWrapper<>();
        query.lambda().set(GoodsRecive::getAuditType,goodsRecive.getAuditType())
                .set(GoodsRecive::getAuditUserId,goodsRecive.getAuditUserId())
                .eq(GoodsRecive::getGoodsReciveId,goodsRecive.getGoodsReciveId());
        if(goodsReciveService.update(query)){
            return ResultUtils.success("操作成功!");
        }
        return ResultUtils.error("操作失败!");
    }
}
